package com.yunyao.framework.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yunyao.common.constant.enums.DataDeletedStatus;
import com.yunyao.common.dto.project.ProjectMemberDto;
import com.yunyao.common.web.enums.ResultMsgEnum;
import com.yunyao.common.web.util.PageResult;
import com.yunyao.common.web.util.YResult;
import com.yunyao.dao.model.BamuProject;
import com.yunyao.dao.model.BamuProjectMember;
import com.yunyao.dao.model.User;
import com.yunyao.dao.service.bamu.IBamuProjectMemberService;
import com.yunyao.dao.service.bamu.IBamuProjectService;
import com.yunyao.dao.service.system.IUserService;
import com.yunyao.framework.dto.ProjectMember.ProjectMemberSaveDto;
import com.yunyao.framework.service.ProjectMemberService;
import com.yunyao.framework.vo.projectMember.ProjectMemberVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class ProjectMemberServiceImpl implements ProjectMemberService{

    @Autowired
    private IBamuProjectMemberService iBamuProjectMemberService;
    @Autowired
    private IBamuProjectService iBamuProjectService;
    @Autowired
    private IUserService userService;
    @Override
    public YResult<PageResult<ProjectMemberVO>> pageList(ProjectMemberDto projectMemberDto) {
        // 构建查询条件
        LambdaQueryWrapper<BamuProjectMember> wrapper = new LambdaQueryWrapper<BamuProjectMember>()
                .like(StringUtils.isNotBlank(projectMemberDto.getProjectId()), BamuProjectMember::getProjectId, projectMemberDto.getProjectId())
                .eq(BamuProjectMember::getIsDeleted, DataDeletedStatus.NO.getType())
                .orderByDesc(BamuProjectMember::getId);
        // 分页查询
        IPage<BamuProjectMember> payPage = new Page<>(projectMemberDto.getPageIndex(), projectMemberDto.getPageSize());
        payPage = iBamuProjectMemberService.page(payPage, wrapper);

        // 处理查询结果
        if (CollectionUtils.isEmpty(payPage.getRecords())) {
            return YResult.success(new PageResult<>());
        }

        // 转换为VO对象列表
        List<ProjectMemberVO> projectVOList = projectDaoToVoList(payPage.getRecords());

        // 返回分页结果
        return YResult.success(new PageResult<>(projectVOList, payPage.getCurrent(), payPage.getSize(), payPage.getPages(), payPage.getTotal()));
    }

    @Override
    public YResult<String> saveInfo(ProjectMemberSaveDto saveDto) {
        BamuProject bamuProject = iBamuProjectService.getById(saveDto.getProjectId());
        if(bamuProject == null){
            return YResult.error("该项目不存在");
        }
        for (int i = 0; i < saveDto.getIdList().size(); i++) {
            User user = userService.getById(saveDto.getIdList().get(i));
            if (user == null) {
                return YResult.error("用户不存在");
            }
            BamuProjectMember bamuProjectMember = new BamuProjectMember();
            bamuProjectMember.setProjectId(saveDto.getProjectId());
            bamuProjectMember.setDeptName(user.getDeptId());
            bamuProjectMember.setUserId(user.getId());
            bamuProjectMember.setUserName(user.getAccount());
            bamuProjectMember.setRealName(user.getNickName());
            bamuProjectMember.setDeptId(user.getDeptId());
            iBamuProjectMemberService.saveOrUpdate(bamuProjectMember);
        }

        return YResult.success("操作成功");
    }

    @Override
    public YResult<Boolean> deleteInfo(String id) {
        // 根据ID查询要删除的BamuPoSupply对象
        BamuProjectMember bamuProjectMember = iBamuProjectMemberService.getByIdNoDel(id);

        // 如果对象不存在，返回错误信息
        if (bamuProjectMember == null) {
            return YResult.error(ResultMsgEnum.NO_DATA);
        }

        return YResult.success(iBamuProjectMemberService.deleteById(id));
    }

    private List<ProjectMemberVO> projectDaoToVoList(List<BamuProjectMember> records) {
        return records.stream().map(this::projectDaoToVo).collect(Collectors.toList());
    }

    private ProjectMemberVO projectDaoToVo(BamuProjectMember bamuProject) {
        ProjectMemberVO projectVO = new ProjectMemberVO();
        BeanUtils.copyProperties(bamuProject, projectVO);
        return projectVO;
    }
}
